package com.panshi.utils;

import com.panshi.entity.*;
import com.panshi.service.SysClientService;
import com.panshi.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminInfo {

    @Autowired
    private SysClientService sysClientService;

    @Autowired
    private UserClientService userClientService;

    //获取角色没有拥有的权限
    public SysRoleT getIsNotRole(int rid){
        SysRoleT role = sysClientService.getRole(rid);
        List<SysRoleFunctionT> roleFunctionID = sysClientService.getRoleFunctionID(rid);
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(-1);
        if(roleFunctionID!=null){
            roleFunctionID.forEach(item->{
                ids.add(item.getFunctionId());
            });
        }
        List<SysFunctionT> funIsNot = sysClientService.getFunIsNot(ids);
        role.setFunctionTList(funIsNot);
        return role;
    }

    //获取角色拥有的权限
    public SysRoleT getByRole(int rid){
        SysRoleT role = sysClientService.getRole(rid);
        List<SysRoleFunctionT> roleFunctionID = sysClientService.getRoleFunctionID(rid);

        List<SysFunctionT> funList = new ArrayList<>();
        roleFunctionID.forEach(item->{
            SysFunctionT functionId = sysClientService.getFunctionId(item.getFunctionId());
            funList.add(functionId);
        });
        role.setFunctionTList(funList);
        return role;
    }



    public void initialize(HttpServletRequest request, Model model, HttpSession session,String statr){
        String userNickname = request.getParameter("userNickname");
        Object pa = session.getAttribute("page"+statr);
        if(pa==null){
            pa = 1;
        }
        int page = (int)pa;

        String type = request.getParameter("type");
        if(type!=null){
            if(type.equals("up")){
                page -=1;
            }else if(type.equals("wn")){
                page +=1;
            }
        }




        List<AppUserT> userSum = userClientService.all();
        model.addAttribute("userSum",userSum.size());

        List<AppUserT> userList = new ArrayList<>();
        if(userNickname!=null && !userNickname.equals("")){
            userList = userClientService.getUserLikeName(userNickname,page,5);
            //    查询全部
        }else{
            userList = userClientService.getUserPageAll(page,5);
        }
        if(userList==null || userList.isEmpty()){
            page-=1;
        }
        if(userList==null){
            userList = new ArrayList<>();
        }
        session.setAttribute("page"+statr,page);
        model.addAttribute("userList",userList);
    }



    public SysAdminApi getAdminInfo(SysAdminApi sysAdminApi){
        SysUserRoleT sysUserRoleT = sysClientService.userRole(sysAdminApi.getAdminId());
        if(sysUserRoleT==null){
            return sysAdminApi;
        }
        SysRoleT role = sysClientService.getRole(sysUserRoleT.getRoleId());
        if(role==null){
            return sysAdminApi;
        }
        sysAdminApi.setSysRoleT(role);
        List<SysRoleFunctionT> roleFunctionID = sysClientService.getRoleFunctionID(role.getRoleId());
        if(roleFunctionID==null){
            return sysAdminApi;
        }
        List<SysFunctionT> funList = new ArrayList<>();
        roleFunctionID.forEach(item->{
            SysFunctionT functionId = sysClientService.getFunctionId(item.getFunctionId());
            funList.add(functionId);
        });
        sysAdminApi.setFunctionTList(funList);
        return sysAdminApi;
    }

    public void sysList(HttpServletRequest request,Model model,HttpSession session,String statr){
        Object pa = session.getAttribute("page"+statr);
        if(pa==null){
            pa = 1;
        }
        int page = (int)pa;

        String type = request.getParameter("type");
        if(type!=null){
            if(type.equals("up")){
                page -=1;
            }else if(type.equals("wn")){
                page +=1;
            }
        }
        String state = request.getParameter("state");

        if(state!=null){
            if(state.equals("用户状态")){
                state = "-1";
            }else if(state.equals("正常")){
                state = "1";
            }else if(state.equals("冻结")){
                state = "0";
            }
        }


        if(state==null || state.equals("")){
            state="-1";
        }
        String useradminname = request.getParameter("useradminname");
        if(useradminname == null || useradminname.length()==0){
            useradminname = "-1";
        }
        //查询全部系统用户
        List<SysAdminApi> adminAll = sysClientService.getAdminAll();
        model.addAttribute("adminSum",adminAll.size());
        System.out.println(useradminname.length());
        System.out.println(state+"   "+useradminname+"  "+page);
        List<SysAdminT> sysList = sysClientService.getAdminLikeName(useradminname,state,page,5);
        sysList.forEach(item->{
            System.out.println("全部信息------------》");
            SysUserRoleT sysUserRoleT = sysClientService.userRole(item.getAdminId());
            if(sysUserRoleT!=null){
                SysRoleT role = sysClientService.getRole(sysUserRoleT.getRoleId());
                item.setSysRoleT(role);
            }else{
                item.setSysRoleT(new SysRoleT());
            }
        });

        System.out.println("系统用户查询");
        sysList.forEach(System.out::println);
        model.addAttribute("sysList",sysList);
    }

    public void funLimitList(HttpServletRequest request,Model model,HttpSession session,String statr){
        Object pa = session.getAttribute("page"+statr);
        if(pa==null){
            pa = 1;
        }
        int page = (int)pa;

        String type = request.getParameter("type");
        if(type!=null){
            if(type.equals("up")){
                page -=1;
            }else if(type.equals("wn")){
                page +=1;
            }
        }
        List<SysFunctionT> funAll = sysClientService.getFunAll();
        model.addAttribute("funSum",funAll.size());

        List<SysFunctionT> limitFun = sysClientService.getLimitFun(page, 5);
        model.addAttribute("limitFun",limitFun);
    }


    public void roleList(HttpServletRequest request,Model model,HttpSession session,String statr){
        Object pa = session.getAttribute("page"+statr);
        if(pa==null){
            pa = 1;
        }
        int page = (int)pa;

        String type = request.getParameter("type");
        if(type!=null){
            if(type.equals("up")){
                page -=1;
            }else if(type.equals("wn")){
                page +=1;
            }
        }


        String rolename = request.getParameter("rolename");
        if(rolename == null || rolename.length()==0){
            rolename = "-1";
        }
        //查询全部系统用户
        List<SysRoleT> roleAll = sysClientService.roleAll();
        model.addAttribute("roleSum",roleAll.size());

        System.out.println(rolename+"   "+roleAll+"  "+page);
        List<SysRoleT> roleList = sysClientService.rolelimit(rolename,page,5);
        if(roleAll!=null){
            roleList.forEach(item->{
                List<SysRoleFunctionT> roleFunctionID = sysClientService.getRoleFunctionID(item.getRoleId());
                List<SysFunctionT> funList = new ArrayList<>();
                roleFunctionID.forEach(it->{
                    SysFunctionT functionId = sysClientService.getFunctionId(it.getFunctionId());
                    funList.add(functionId);
                });
                item.setFunctionTList(funList);
            });

        }
        System.out.println("系统用户查询");
        roleList.forEach(System.out::println);
        model.addAttribute("roleList",roleList);
    }


}
